package bb.lanxing.lib.devices.sprint;

import bb.lanxing.lib.devices.utils.ByteUtils;
import java.nio.ByteBuffer;

public class Commands {
    public static final byte CMD_CONTENT_RESET_DISTANCE = 0;
    public static final byte CMD_CONTENT_RESET_ALL = 1;
    public static final byte CMD_CONTENT_RESET_BOND = 2;
    public static final byte CMD_CONTENT_PAUSE_RECORD = 3;
    public static final byte CMD_CONTENT_START_RECORD = 1;
    public static final byte CMD_CONTENT_STOP_RECORD = 2;

    public static final byte CMD_RESET_HEARTRATE_DEVICE = -52;
    public static final byte CMD_RESET_HEARTRATE_DEVICE_RESPONSE = -51;
    public static final byte CMD_HEARTRATE_DEVICE_SUPPORT_TYPE = 112;
    public static final byte CMD_HEARTRATE_ALERT_X2P_DEVICE = 113;
    public static final byte CMD_RECORD_STEP_X2P_DEVICE = 114;
    public static final byte CMD_START_RECORD_HEARTRATE = 117;
    public static final byte CMD_HEARTRATE_RECORD_MODE = 118;

    public static final byte DEVICE_MEMORY_CAPACITY = 10;
    public static final byte ENTER_DFU_MODE = -33;
    public static final byte ERROR_FILE_NOT_EXIST_CRASH = 12;
    public static final byte FILE_ANALYSIS_FAILED = 21;
    public static final byte FORCE_STOP = 117;
    public static final byte GET_CURRENT_STATUS = -1;

    public static final byte STOP = -1;
    public static final byte RECORDING = 0;
    public static final byte PAUSING = 1;
    public static final byte IDLE = 4;
    public static final byte GET_FILE = 5;
    public static final byte POSTING = 6;
    public static final byte POST_FILE = 7;
    public static final byte RECEIVING = 8;
    public static final byte DEL_SINGLE_FILE = 13;
    public static final byte DEL_FILE_ON_SUCCESSFUL = 14;
    public static final byte DEL_MULTIP_FILES = 15;
    public static final byte VERIFICATION_FAILED = 17;
    public static final byte FILE_NOT_FOUNT = 18;
    public static final byte ABORT_TREANSFER = 31;


    private static final int MAX_CHECKSUM_LEN = 1;
    private static final int MAX_CONTENT_LEN = 18;
    private static final int MAX_OPCODE_LEN = 1;
    private static final int MIN_CONTENT_LEN = 1;


    public static final byte MOTION = -84;
    public static final byte OUT_OF_MEMORY = 19;
    public static final byte NOT_IDLE = 20;
    public static final byte POST_USER_ID = 64;
    public static final byte RESET_DEVICE = -52;


    public static final byte REQUEST_MEMORY_CAPACITY = 9;
    public static final byte REQUEST_GNSS_EXPIRED_TIME = 119;

    public static final byte RESPONSE_UPDATE_UTC_TIME = 85;
    public static final byte RESPONSE_GNSS_EXPIRED_TIME = 120;

    public static final byte SWITCHABLE_CADENCE_SWITCH = 48;
    public static final byte SWITCHABLE_CADENCE_TYPE = 49;
    public static final byte UPDATE_UTC_TIME = 84;


    public static Command create(byte[] bArr) {
        Command command = new Command();
        command.cmd = bArr[0];
        command.checksum = bArr[bArr.length - 1];
        int length = (bArr.length - 1) - 1;
        if (ByteUtils.assertNotNull(bArr)) {
            command.content = new byte[length];
            System.arraycopy(bArr, 1, command.content, 0, length);
        } else {
            command.content = null;
        }
        return command;
    }

    public static Response createResponse(byte[] bArr) {
        Response response = new Response();
        response.raw = bArr;
        response.cmd = bArr[0];
        response.checksum = bArr[bArr.length - 1];
        int length = (bArr.length - 1) - 1;
        if (length > 0) {
            response.content = new byte[length];
            System.arraycopy(bArr, 1, response.content, 0, length);
        } else {
            ByteBuffer allocate = ByteBuffer.allocate(1);
            allocate.put((byte) 1);
            response.content = allocate.array();
        }
        return response;
    }

    public static Command create(int i, byte[] bArr) {
        if (bArr == null) {
            bArr = new byte[]{0};
        }
        Command command = new Command();
        command.cmd = (byte) i;
        command.content = bArr;
        command.checksum = (byte) (i ^ checksum(bArr, 0, bArr.length));
        return command;
    }

    public static byte[] createRaw(byte b, byte[] bArr) {
        int length = ByteUtils.assertNotNull(bArr) ? bArr.length : 1;
        byte[] bArr2 = new byte[length + 1 + 1];
        bArr2[0] = b;
        if (ByteUtils.assertNotNull(bArr)) {
            System.arraycopy(bArr, 0, bArr2, 1, length);
        } else {
            bArr2[1] = 0;
        }
        bArr2[bArr.length - 1] = checksum(bArr2, 0, MAX_CONTENT_LEN);
        return bArr2;
    }

    public static byte[] toBytes(Command command) {
        int length = command.content.length + 1 + 1;
        byte[] bArr = new byte[length];
        bArr[0] = command.cmd;
        if (ByteUtils.assertNotNull(command.content)) {
            System.arraycopy(command.content, 0, bArr, 1, command.content.length);
        }
        bArr[length - 1] = command.checksum;
        return bArr;
    }

    public static byte checksum(byte[] bArr, int i, int i2) {
        if (bArr == null || bArr.length == 0 || i < 0 || i2 < 0) {
            return (byte) 0;
        }
        byte b = 0;
        for (int i3 = 0; i3 < i2; i3++) {
            int i4 = i + i3;
            if (i4 >= bArr.length) {
                break;
            }
            b = (byte) (b ^ bArr[i4]);
        }
        return b;
    }

    public static String parseFileName(byte[] bArr) {
        String str = "";
        if (bArr == null) {
            return str;
        }
        byte[] content = create(bArr).getContent();
        if (content != null) {
            str = new String(content);
        }
        return str.trim();
    }

    public static String parseFileName(Command command) {
        String str = "";
        if (command == null) {
            return str;
        }
        byte[] content = command.getContent();
        if (content != null) {
            str = new String(content);
        }
        return str.trim();
    }

    public static String toString(Command command) {
        return "cmd: " + ByteUtils.toHex(command.cmd) + "\nData: " + ByteUtils.printlnByteArrayToUnsignHexString(command.content) + "\nChecksum: " + ByteUtils.toHex(command.checksum) + "\n";
    }

    public static String toString(byte[] bArr) {
        return "cmd: " + ByteUtils.toHex(bArr[0]) + "\nData: " + ByteUtils.printlnByteArrayToUnsignHexString(bArr, 1, (bArr.length - 1) - 1) + "\nChecksum: " + ByteUtils.toHex(bArr[bArr.length - 1]) + "\n";
    }
}
